package com.digitalchina.lxnsy.activity.farmTrain;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.webkit.JavascriptInterface;

import com.digitalchina.lxnsy.base.BaseApi;
import com.digitalchina.lxnsy.base.BaseApplication;
import com.digitalchina.lxnsy.base.BaseHtmlActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.FileVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;

import java.util.ArrayList;

/**
 * ================================================
 * 项目名称：android
 * 类描述：新增成功案例
 * 创建人：rxx
 * 创建时间：2020/1/15 17:26
 * 修改备注：
 * ================================================
 */
public class TrainAddCaseActivity extends BaseHtmlActivity {


    private String json = "";
    private String imgId = ""; // 服务端返回的fileName

    public static final int REQUEST_CODE_SELECT = 100;
    String index = "";
    long size;
    String name = "";

    @Override
    protected void loadHtml() {
        webView.addJavascriptObject(new JsApi(), null);
        loadUrl("train-add-case");
        setTitle("新增成功案例");
        json = (String) getIntent().getSerializableExtra("msg");
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new BaseApplication.GlideImageLoader());   //设置图片加载器
        imagePicker.setCrop(false);        //允许裁剪（单选才有效）
        imagePicker.setMultiMode(false);    //是否多选模式

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG.upload: // 上传图
                        dismissDialog();
                        FileVO fileVO = FastJsonUtil.getBean(msg.obj.toString(), "fileStorage", FileVO.class);
                        if (fileVO == null)
                            return false;
                        imgId = fileVO.getName();
                        // 传给html图片绝对路径
                        callJsMethod("showImg", new String[]{index, imgId, name, String.valueOf(size)});
                        break;
                    case MSG.upload_failed:
                        dismissDialog();
                        showToast((String) msg.obj);
                        break;
                }
                return false;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT) {
                showDialog("上传中,请稍候!");
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                size = images.get(0).size;
                name = images.get(0).name;
                BaseApi.upload(activity, true, images.get(0).path, mHandler);
            }
        }
    }


    public class JsApi {

        @JavascriptInterface
        public String getJson(Object msg) {
            return json;
        }

        @JavascriptInterface
        public void save(Object msg) {
            activity.setResult(RESULT_OK);
        }

        /**
         * 跳转至自定义相册
         */
        @JavascriptInterface
        public void selectImage(Object msg) {

            index = msg.toString();
            Intent intent = new Intent(activity, ImageGridActivity.class);
            activity.startActivityForResult(intent, REQUEST_CODE_SELECT);
        }
    }
}
